package com.converter.huawei.utils;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.converter.huawei.vo.LoginVO;
import com.converter.huawei.vo.back.BackUrlResultVO;
import com.converter.huawei.vo.pictrue.*;
import io.swagger.models.auth.In;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.util.*;
import java.util.stream.Collectors;

public class HuaweiUtils {

    public static Map<String,Object> param= new HashMap();

    private static String ip = "192.168.10.252";
//    private static String ip = "192.168.10.211";

//    private static String serverUrl = "https://192.168.10.211";
    private static String serverUrl = "https://192.168.10.252";

    private static String rangeUrl = "/API/Login/Range";

    private static String deviceInfoUrl = "/API/Login/DeviceInfo/Get";

    private static String channelInfoUrl = "/API/Login/ChannelInfo/Get";
    /**
     * 用在用户首次登录或者密码过期时修改密码
     */
    private static String loginSetPwd = "/API/Login/Password/Set";
    /**
     * 用在设备首次开机时设置admin用户的密码
     */
    private static String firstLoginSetPwd = "/API/FirstLogin/Password/Set";

    /**
     * 登录
     */
    private static String webLogin = "/API/Web/Login";

    /**
     * 登录
     */
    private static String heartbeat = "/API/Login/Heartbeat";


    /**
     * 获取授权token
     */
    private static String getLoginToken = "/API/Get_Login_Token";

    /**
     * 获取直播流地址
     */
    private static String GetRtspUrl = "/API/Preview/GetRtspUrl/Get";
    /**
     * 获取回放流地址
     */
    private static String getPlaybacRtspkUrl = "/API/Playback/PlaybacRtspkUrl/Get";

    /**
     * 获取告警图片
     */
    private static String picture = "/API/Playback/Picture/Get";

    /**
     * 获取告警录像
     */
    private static String videoAlarm = "/API/Playback/SearchRecord/Search";
    /**
     * 获取目标检测,抓拍数量
     */
    private static String snapedFaces = "/API/AI/SnapedFaces/Search";

    /**
     * 获取目标检测,抓拍数量
     */
    private static String snapedFacesGet = "/API/AI/SnapedFaces/GetByIndex";

    /**
     * 获取录像下载流
     */
    private static String getDownloadRtspkUrlApi = "/API/Playback/DownloadRtspkUrl/Get";

    /**
     * 摄像头方向控制
     */
    private static String getCameraControlUrlApi = "/API/PreviewChannel/PTZ/Control";


    /**
     * 控制摄像头旋转
     * @param channel
     * @param code
     * @return
     */
    public static String cameraControl(String channel,String code){

        String userName = "admin";
        String ip = "192.168.1.50";
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get(ip+"#"+ userName +"cookie")+"");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get(ip+"#"+ userName +"csrftoken")+"");
        Map<String, Object> data = new HashMap();
        data.put("channel", channel);
        data.put("cmd", Integer.valueOf(code));
        data.put("speed", 1);
        data.put("ctl_stop", false);

//        data.put("record_type", 1);
        Map<String, Object> bodyMap = new HashMap();
        bodyMap.put("data", data);
        String body = JSONUtil.toJsonStr(bodyMap);
        String res = HttpRequest.post("https://"+"192.168.1.50"+getCameraControlUrlApi).headerMap(header,true).body(body).execute().body();
        BackUrlResultVO vo = JSONUtil.toBean(res, BackUrlResultVO.class);
        if("success".equals(vo.getResult())) {
            data.put("ctl_stop", true);
            bodyMap.put("data", data);
            body = JSONUtil.toJsonStr(bodyMap);
            res = HttpRequest.post("https://"+"192.168.1.50"+getCameraControlUrlApi).headerMap(header,true).body(body).execute().body();
        }else {
            return res;
        }
        return res;

    }


    public static String getDownloadRtspkUrl(String startTime,String endTime){
        startTime = HuaWeiDateUtil.timeZoneTransfer(startTime,"yyyy-MM-dd HH:mm:ss","+8","0");
        endTime = HuaWeiDateUtil.timeZoneTransfer(endTime,"yyyy-MM-dd HH:mm:ss","+8","0");
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get(ip + "cookie") + "");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get(ip + "csrftoken") + "");
        Map<String, Object> data = new HashMap();
        data.put("channel", "CH1");
        data.put("transport_type","tcp");
        data.put("stream_mode","Mainstream");
        data.put("start_date", startTime.split(" ")[0]);
        data.put("start_time",startTime.split(" ")[1]);
        data.put("end_date",endTime.split(" ")[0]);
        data.put("end_time",endTime.split(" ")[1]);
        data.put("record_type", 1);
        Map<String, Object> bodyMap = new HashMap();
        bodyMap.put("data", data);
        String body = JSONUtil.toJsonStr(bodyMap);
        String res = HttpRequest.post(serverUrl + getDownloadRtspkUrlApi).headerMap(header, true).body(body).execute().body();
        return res;
    }


    /**
     * 登录前 获取登录前设备信息
     */
    public static String range(){
        String res = HttpUtil.createPost(serverUrl+rangeUrl).execute().body();
        return res;
    }

    /**
     * 登录前 获取设备信息
     */
    public static String deviceInfo(){
        String res = HttpUtil.post(serverUrl+deviceInfoUrl,new HashMap<>());
        return res;
    }

    /**
     * 登录前 获取通道信息
     */
    public static String channelInfoUrl(){
        String res = HttpUtil.post(serverUrl+channelInfoUrl,new HashMap<>());
        return res;
    }

    /**
     * 用在用户首次登录或者密码过期时修改密码
     */
    public static String loginSetPwd(){
        String res = HttpUtil.post(serverUrl+loginSetPwd,new HashMap<>());
        return res;
    }

    /**
     * 获取直播流
     * 1、is_dualtalk字段和is_device字段同时传入true时，返回nvr设备对讲URL。注意，is_device不能单独传入，否则只返回通道主子码流URL；
     * 2、is_dualtalk字段传true且is_device字段不存在或传false时，返回通道对讲URL；
     * 3、is_dualtalk字段不传或传false，且is_metadata字段传true，返回通道元数据URL；
     * 4、is_dualtalk字段不传或传false，is_extenxion传true，返回通道元数据扩展URL。
     * is_device、is_dualtalk、is_metadata和is_extenxion字段不传或都传false时，返回通道主子码流URL。
     */
    public static String GetRtspUrl(String ip,String[] channel, String userName){
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get(ip+"#"+ userName +"cookie")+"");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get(ip+"#"+ userName +"csrftoken")+"");
        Map<String, Object> data = new HashMap();
        data.put("channel", Arrays.asList(channel));
        data.put("transport_type","tcp");

        data.put("is_dualtalk", "true");

        Map<String, Object> bodyMap = new HashMap();
        bodyMap.put("data",data);
        String body = JSONUtil.toJsonStr(bodyMap);
        String res = HttpRequest.post("https://"+ip+GetRtspUrl).headerMap(header,true).body(body).execute().body();
        return res;
    }

    /**
     * 获取回放流地址
     */
    public static String getPlaybacRtspkUrl(String ip,String channel,String startTime,String endTime,String userName){
//        startTime = HuaWeiDateUtil.timeZoneTransfer(startTime,"yyyy-MM-dd HH:mm:ss","+8","0");
//        endTime = HuaWeiDateUtil.timeZoneTransfer(endTime,"yyyy-MM-dd HH:mm:ss","+8","0");
        startTime = HuaWeiDateUtil.timeZoneTransfer(startTime,"yyyy-MM-dd HH:mm:ss","+8","0");
        endTime = HuaWeiDateUtil.timeZoneTransfer(endTime,"yyyy-MM-dd HH:mm:ss","+8","0");
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get(ip+"#"+ userName +"cookie")+"");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get(ip+"#"+ userName +"csrftoken")+"");
        Map<String, Object> data = new HashMap();
        data.put("channel", channel);
        data.put("transport_type","tcp");
        data.put("stream_mode","Mainstream");
        data.put("start_date", startTime.split(" ")[0]);
        data.put("start_time",startTime.split(" ")[1]);
        data.put("end_date",endTime.split(" ")[0]);
        data.put("end_time",endTime.split(" ")[1]);
        data.put("record_type", 4294967295L);

        Map<String, Object> bodyMap = new HashMap();
        bodyMap.put("data",data);
        String body = JSONUtil.toJsonStr(bodyMap);
        String res = HttpRequest.post("https://"+ip+getPlaybacRtspkUrl).headerMap(header,true).body(body).execute().body();
        return res;
    }

    /**
     * 获取回放流地址
     */
//    public static String getPlaybacRtspkUrl(String ip,String channel,String startTime,String endTime){
////        startTime = HuaWeiDateUtil.timeZoneTransfer(startTime,"yyyy-MM-dd HH:mm:ss","+8","0");
////        endTime = HuaWeiDateUtil.timeZoneTransfer(endTime,"yyyy-MM-dd HH:mm:ss","+8","0");
//        Map<String, String> header = new HashMap();
//        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
//        header.put("Content-Type", "application/json;charset=utf-8");
//        header.put("Cookie", param.get(ip+"cookie")+"");
//        header.put("Content-Length", "0");
//        header.put("X-csrftoken", param.get(ip+"csrftoken")+"");
//        Map<String, Object> data = new HashMap();
//        data.put("channel", channel);
//        data.put("transport_type","tcp");
//        data.put("stream_mode","Mainstream");
//        data.put("start_date", startTime.split(" ")[0]);
//        data.put("start_time",startTime.split(" ")[1]);
//        data.put("end_date",endTime.split(" ")[0]);
//        data.put("end_time",endTime.split(" ")[1]);
//        data.put("record_type", 4294967295L);
//
//        Map<String, Object> bodyMap = new HashMap();
//        bodyMap.put("data",data);
//        String body = JSONUtil.toJsonStr(bodyMap);
//        String res = HttpRequest.post("https://"+ip+getPlaybacRtspkUrl).headerMap(header,true).body(body).execute().body();
//        return res;
//    }

    /**
     * 获取告警录像
     */
    public static String videoAlarm(String ip) {
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get(ip + "cookie") + "");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get(ip + "csrftoken") + "");
        Map<String, Object> data = new HashMap();
        List<String> list = new ArrayList<>();
        list.add("CH1");
//        data.put("channel", list);
        data.put("start_date", "07/24/2022");
        data.put("start_time", "00:00:00");
        data.put("end_date", "07/29/2022");
        data.put("end_time", "23:59:59");
//        data.put("record_type", 524287L);

        Map<String, Object> bodyMap = new HashMap();
        bodyMap.put("data", data);
        String body = JSONUtil.toJsonStr(bodyMap);
        String res = HttpRequest.post(serverUrl + videoAlarm).headerMap(header, true).body(body).execute().body();
        return res;
    }

    /**
     * 获取目标检测抓拍图片
     */
    public static PicPage SnapedFaces(String ip,String channel,Integer startIndex,String startTime,String endTime){
//        startTime = HuaWeiDateUtil.timeZoneTransfer(startTime,"yyyy-MM-dd HH:mm:ss","+8","0");
//        endTime = HuaWeiDateUtil.timeZoneTransfer(endTime,"yyyy-MM-dd HH:mm:ss","+8","0");
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get(ip+"cookie")+"");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get(ip+"csrftoken")+"");
        Map<String, Object> data = new HashMap();
        List<Integer> list = new ArrayList<>();
        String c = channel.replace("CH","");
        list.add(Integer.parseInt(c));
        data.put("Chn", list);
        data.put("StartTime",startTime);
        data.put("EndTime",endTime);
        Map<String, Object> bodyMap = new HashMap();
        bodyMap.put("data",data);
        String body = JSONUtil.toJsonStr(bodyMap);
        String res = HttpRequest.post("https://"+ip+snapedFaces).headerMap(header,true).body(body).execute().body();
        SnapedFacesDateVO vo = JSONUtil.toBean(res,SnapedFacesDateVO.class);
        return SnapedFacesGet(ip, vo.getData().getCount());
    }

    /**
     * 获取目标检测抓拍图片
     */
    public static PicPage SnapedFacesGet(String ip, Integer count){
        PicPage page = new PicPage();
        List<SnapedFacesPicInfoVO> normaList = new LinkedList<>();
        List<SnapedFacesPicInfoVO> notNormaList =  new LinkedList<>();
        int c = (int)Math.ceil((double)count/30);
        for(int i=1;i<=c;i++){
            Map<String, String> header = new HashMap();
            header.put("Accept", "application/json, text/javascript, */*; q=0.01");
            header.put("Content-Type", "application/json;charset=utf-8");
            header.put("Cookie", param.get(ip+"cookie")+"");
            header.put("Content-Length", "0");
            header.put("X-csrftoken", param.get(ip+"csrftoken")+"");
            Map<String, Object> data = new HashMap();
            data.put("StartIndex",(i-1)*30);
            data.put("Count",i==c?(count%30):30);
//        data.put("WithFaceImage",1);
            data.put("WithBackgroud",1);
//        data.put("WithBodyImage",1);
//        data.put("WithFeature",1);

            Map<String, Object> bodyMap = new HashMap();
            bodyMap.put("data",data);
            String body = JSONUtil.toJsonStr(bodyMap);
            String res = HttpRequest.post("https://"+ip+snapedFacesGet).headerMap(header,true).body(body).execute().body();
            SnapedFacesDateVO vo = JSONUtil.toBean(res,SnapedFacesDateVO.class);
            List<SnapedFacesPicInfoVO> picInfoVOList = vo.getData().getSnapedFaceInfo();

            for(SnapedFacesPicInfoVO info:picInfoVOList){
                if(StrUtil.isNotEmpty(info.getMatchedFaceId())){
//                正常进入
                    normaList.add(info);
                }else{
//                不正常进入
                    notNormaList.add(info);
                }
            }
        }
//        normaList.stream().sorted()
//        page.setTotal(vo.getData().getTotalCount());
//        normaList = normaList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
//                () -> new TreeSet<>(Comparator.comparing(SnapedFacesPicInfoVO::getUUId))), ArrayList::new));
//        notNormaList = notNormaList.stream().collect(Collectors.collectingAndThen(Collectors.toCollection(
//                () -> new TreeSet<>(Comparator.comparing(SnapedFacesPicInfoVO::getUUId))), ArrayList::new));
        normaList = normaList.stream().sorted(Comparator.comparing(SnapedFacesPicInfoVO::getStartTime).reversed()).collect(Collectors.toList());
        notNormaList = notNormaList.stream().sorted(Comparator.comparing(SnapedFacesPicInfoVO::getStartTime).reversed()).collect(Collectors.toList());
        page.setNormalList(normaList);
        page.setNotNormalList(notNormaList);
        return page;
    }

    /**
     * 获取告警图片列表
     */
    public static String picture(String ip){
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get(ip+"cookie")+"");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get(ip+"csrftoken")+"");
        Map<String, Object> data = new HashMap();
        List<String> list = new ArrayList<>();
        list.add("CH1");
        data.put("channel", list);
        data.put("start_date", "07/24/2022");
        data.put("start_time","00:00:00");
        data.put("end_date","07/29/2022");
        data.put("end_time","23:59:59");
        data.put("record_type", 524287L);

        Map<String, Object> bodyMap = new HashMap();
        bodyMap.put("data",data);
        String body = JSONUtil.toJsonStr(bodyMap);
        String res = HttpRequest.post("https://"+ip+picture).headerMap(header,true).body(body).execute().body();
        PictureVO vo = JSONUtil.toBean(res, PictureVO.class);
        List<PictureInfoVO> picInfoList = vo.getData().getAll_pic_info();
        List<Map<String,String>> picMapList = new ArrayList<>();
        for(PictureInfoVO infoVO:picInfoList){
            Map<String,String> picMap = new HashMap<>();
            String picId = infoVO.getPic_info();
            String picRes = HuaweiUtils.showPic(picId,ip);
            PictureDetailVO picVO = JSONUtil.toBean(picRes, PictureDetailVO.class);
            PictureDetailInfoVO picDetail = picVO.getData().getPicture();
            picMap.put("base64Img",picDetail.getImage());
            picMap.put("time",picDetail.getTime());
            picMapList.add(picMap);
        }

        return JSONUtil.toJsonStr(picMapList);
    }

    /**
     * 查看单个告警图片
     */
    public static String showPic(String picId ,String ip){
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get(ip+"cookie")+"");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get(ip+"csrftoken")+"");
        Map<String, Object> data = new HashMap();
        data.put("pic_info",picId);
        data.put("msg_type","send_0");

        Map<String, Object> bodyMap = new HashMap();
        bodyMap.put("data",data);
        String body = JSONUtil.toJsonStr(bodyMap);
        String res = HttpRequest.post("https://"+ip+picture).headerMap(header,true).body(body).execute().body();
        return res;
    }

    /**
     * 获取授权token
     */
    public static String getToken(){
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get("cookie")+"");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get("csrftoken")+"");
        Map<String, Object> data = new HashMap();
        data.put("limit_ip",false);
        String body = JSONUtil.toJsonStr(data);
        String res = HttpRequest.post(serverUrl+getLoginToken).headerMap(header,true).body(body).execute().body();
//        String res = HttpUtil.post(serverUrl+getLoginToken,new HashMap<>());
        return res;
    }
    /**
     * 摘要登录
     */
    public static LoginVO webLogin(String ip,String userName, String password){
        //        登出
            logout(ip);
        LoginVO vo = new LoginVO();
        try {
            HttpResponse res = postMethod(ip,"https://"+ip+webLogin,443,userName,password);
            // 连接成功
            vo.setCode(res.getStatusLine().getStatusCode()+"");
            if (200 == res.getStatusLine().getStatusCode()) {
                // org.apache.http.Header;
                //获取响应头，遍历 取出参数
                Header[] headers = res.getAllHeaders();
                for (Header header : headers) {
                    if ("Set-Cookie".equals(header.getName())) {
                        //返回 Set-Cookie 的值
                        vo.setCookie(header.getValue());
                        param.put(ip+"#"+userName+"cookie",header.getValue());
                    }
                    if ("X-csrftoken".equals(header.getName())) {
                        //返回 X-csrftoken 的值
                        vo.setCsrftoken(header.getValue());
                        param.put(ip+"#"+userName+"csrftoken",header.getValue());
                    }
                }
            } else {
                System.out.println("连接失败");
            }
            vo.setMsg(EntityUtils.toString(res.getEntity(), "utf-8"));
            return vo;
        } catch (Exception e) {
            e.printStackTrace();
            return vo;
        }
    }

//    /**
//     * Digest Auth 摘要认证
//     * @param ip
//     * @param url
//     * @param userName
//     * @param password
//     * @param query
//     * @return
//     */
//    public static HttpResponse postMethod(String ip,String url, String userName,String password,String query) {
//        try {
//
//            CredentialsProvider credsProvider = new BasicCredentialsProvider();
//            credsProvider.setCredentials(
//                    new AuthScope(ip, 80),//请求地址 + 端口号
//                    new UsernamePasswordCredentials(userName, password));// 用户名 + 密码 （用于验证）
//            CloseableHttpClient httpclient = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
//            HttpPost postMethod = new HttpPost(url);//请求详细地址（如：http://192.168.1.105:9000/MotorVehicles）
//            StringEntity s = new StringEntity(query);//向后台传的json数据
//            s.setContentEncoding("utf-8");//编码
//            s.setContentType("application/json");//发送json数据需要设置contentType
//            postMethod.setEntity(s);
//            HttpResponse response = httpclient.execute(postMethod); //执行POST方法
//
//            System.out.println("resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
//            System.out.println("result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容
//            return response;
//        } catch (Exception e) {
//            System.out.println("推送失败："+e);
//            return null;
//        }
//
//    }
    /**
     * Digest Auth 摘要认证
     * @param ip
     * @param url
     * @param userName
     * @param password
     * @return
     */
    public static HttpResponse postMethod(String ip,String url,Integer port, String userName,String password) {
        try {

            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(
                    new AuthScope(ip, port),//请求地址 + 端口号
                    new UsernamePasswordCredentials(userName, password));// 用户名 + 密码 （用于验证）

            SSLContext sslContext = new SSLContextBuilder()
                    .loadTrustMaterial(null, (certificate, authType) -> true).build();

            CloseableHttpClient client = HttpClients.custom()
                    .setDefaultCredentialsProvider(credsProvider)
                    .setSSLContext(sslContext)
                    .setSSLHostnameVerifier(new NoopHostnameVerifier())
                    .build();

            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/json;charset=utf-8");

            HttpResponse response = client.execute(httpPost);
            return response;
        } catch (Exception e) {
            System.out.println("推送失败："+e);
            return null;
        }

    }

    /**
     * 心跳保活
     */
    public static String heartBeat(String ip,String userName){
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get(ip+"#"+userName+"cookie")+"");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get(ip+"#"+userName+"csrftoken")+"");

        String res = HttpRequest.post("https://"+ip+heartbeat).headerMap(header,true).execute().body();
        return res;
    }

    /**
     * 登出
     */
    public static String logout(String ip){
        Map<String, String> header = new HashMap();
        header.put("Accept", "application/json, text/javascript, */*; q=0.01");
        header.put("Content-Type", "application/json;charset=utf-8");
        header.put("Cookie", param.get(ip+"cookie")+"");
        header.put("Content-Length", "0");
        header.put("X-csrftoken", param.get(ip+"csrftoken")+"");

        String res = HttpRequest.post("https://"+ip+"/API/Web/Logout").headerMap(header,true).execute().body();
        System.out.println(ip + "=============="+res);
        return res;
    }
}
